﻿using Routine.Api.Entities;
using Routine.Api.ValidationAttributes;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Routine.Api.Models
{
    [EmployeeNoMustDifferentFromFirstName(ErrorMessage ="员工的编号必须和名不一样!!!")]//应用自定义的Attibute
    public class EmployeeAddDto : IValidatableObject
    {
        ////  public Guid Id { get; set; }  //创建资源里面不需要有这三个属性；
        ////  public Guid CompanyId { get; set; }

        [Display(Name = "员工号")]
        [Required(ErrorMessage = "{0}是必填项")]
        [StringLength(10, MinimumLength = 10, ErrorMessage = "{0}的长度是{1}")]
        public string EmployeeNo { get; set; }    //前端写的时候，是用的驼峰法，C#每个单词首字母都大写；

        [Display(Name = "名")]
        [Required(ErrorMessage = "{0}是必填项")]
        [MaxLength(50, ErrorMessage = "{0}的长度不能超过{1}")]
        public string FirstName { get; set; }            //这个AddDto改动就相对Dto差别就比较大了；

        [Display(Name = "姓"), Required(ErrorMessage = "{0}是必填项"), MaxLength(50, ErrorMessage = "{0}的长度不能超过{1}")]
        public string LastName { get; set; }

        [Display(Name = "性别")]
        public Gender Gender { get; set; }      //性别是枚举类型

        [Display(Name = "出生日期")]
        public DateTime DateOfBirth { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (FirstName == LastName)
            {
                yield return new ValidationResult("姓和名不能一样",
                    new[] { nameof(FirstName), nameof(LastName) });//这是类级别的验证，或者跨属性的验证；
            }
            //yield是用来返回枚举类型。只能用于IEnumerable；
            //yield return 一次一个的返回， yield break，终止迭代;(复习yield和遍历)
        }
    }
}
